load("@poetry//:dependencies.bzl", "dependency")

py_binary(
    name = "codeowners",
    srcs = [
        "codeowners_generate.py",
        "parsers/owners_v1.py",
        "parsers/owners_v2.py",
        "validate_codeowners.py",
        "//utils:evergreen_git.py",
    ],
    data = [
        "@codeowners_binary//:codeowners",
        "@codeowners_validator//:codeowners-validator",
    ],
    env = {
        "CODEOWNERS_VALIDATOR_PATH": "$(rootpath @codeowners_validator//:codeowners-validator)",
        "CODEOWNERS_BINARY_PATH": "$(rootpath @codeowners_binary//:codeowners)",
    } | select({
        ":add_auto_approve_user": {
            "ADD_AUTO_APPROVE_USER": "true",
        },
        "//conditions:default": {},
    }) | select({
        ":dont_check_new_files": {
            "CODEOWNERS_CHECK_NEW_FILES": "false",
        },
        "//conditions:default": {},
    }) | select({
        ":have_allowed_unowned_files": {
            "ALLOWED_UNOWNED_FILES_PATH": "$(codeowners_allowed_unowned_files_path)",
        },
        "//conditions:default": {},
    }) | select({
        ":have_default_owner": {
            "CODEOWNERS_DEFAULT_OWNER": "$(codeowners_default_owner)",
        },
        "//conditions:default": {},
    }) | select({
        ":have_banned_codeowners": {
            "BANNED_CODEOWNERS_FILE_PATH": "$(codeowners_banned_codeowners_file_path)",
        },
        "//conditions:default": {},
    }),
    main = "codeowners_generate.py",
    visibility = ["//visibility:public"],
    deps = [
        dependency(
            "pyyaml",
        ),
        dependency(
            "gitpython",
        ),
    ],
)

config_setting(
    name = "add_auto_approve_user",
    define_values = {
        "codeowners_add_auto_approve_user": "True",
    },
)

config_setting(
    name = "dont_check_new_files",
    define_values = {
        "codeowners_dont_check_new_files": "True",
    },
)

config_setting(
    name = "have_allowed_unowned_files",
    define_values = {
        "codeowners_have_allowed_unowned_files": "True",
    },
)

config_setting(
    name = "have_default_owner",
    define_values = {
        "codeowners_have_default_owner": "True",
    },
)

config_setting(
    name = "have_banned_codeowners",
    define_values = {
        "codeowners_have_banned_codeowners": "True",
    },
)

py_binary(
    name = "check_github_codeowner_errors",
    srcs = [
        "check_github_codeowner_errors.py",
    ],
    data = [],
    env = {},
    main = "check_github_codeowner_errors.py",
    visibility = ["//visibility:public"],
    deps = [
        dependency(
            "requests",
        ),
        dependency(
            "structlog",
        ),
        dependency(
            "pyyaml",
        ),
    ],
)
